///	<summary>
///	  Main form of demo project for DAttribute's custom attributes libraries.
///	</summary>
unit Unit1;

(*******************************************************************************
  MIT License

  Copyright (c) 2013 - Luthfi Harahap

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE
*******************************************************************************)

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SimpleConfig, Vcl.Buttons, Vcl.StdCtrls,
  Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    edtDir: TEdit;
    SpeedButton1: TSpeedButton;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Edit2: TEdit;
    Label3: TLabel;
    udDefaultDay: TUpDown;
    BitBtn1: TBitBtn;
    cbbDefCity: TComboBox;
    Label2: TLabel;
    BitBtn4: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
  private
    { Private declarations }
    procedure WriteConfig;
    procedure ReadConfig;
  public
    Config: TMyConfig;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  FileCtrl
  , DAttributes.Validations;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Config.Reset;
  ReadConfig;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  WriteConfig;
  Config.Validate;
  Config.SaveToIni(ChangeFileExt(ParamStr(0), '.ini'));
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
  Config.LoadFromIni(ChangeFileExt(ParamStr(0), '.ini'));
  ReadConfig;
end;

procedure TForm1.BitBtn4Click(Sender: TObject);
begin
  WriteConfig;
  try
    Config.Validate;
  except
    on E:EValidationError do
    begin
      if LowerCase(E.PropName)='workingdir' then
        edtDir.SetFocus
      else if LowerCase(E.PropName)='defaultday' then
        udDefaultDay.Associate.SetFocus
      else if LowerCase(E.PropName)='defaultcity' then
        cbbDefCity.SetFocus;
      raise;
    end
    else
      raise;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Config := TMyConfig.Create;
  ReadConfig;
end;

procedure TForm1.ReadConfig;
begin
  edtDir.Text := Config.WorkingDir;
  udDefaultDay.Position := Config.DefaultDay;
  cbbDefCity.ItemIndex := cbbDefCity.Items.IndexOf(Config.DefaultCity);
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  vDir: string;
begin
  vDir := edtDir.Text;
  if SelectDirectory(vDir, [], 0) then
    edtDir.Text := vDir;
end;

procedure TForm1.WriteConfig;
begin
  Config.WorkingDir := edtDir.Text;
  Config.DefaultDay := udDefaultDay.Position;
  Config.DefaultCity := cbbDefCity.Text;
end;

end.
